<template>
    <h2>会员权益添加页面</h2>
    <table border="1">
        <tr>
            <td>权益名称</td>
            <td>
                <input type="text" v-model="right.mR_Name"/>
            </td>
        </tr>
        <tr>
            <td>权益说明</td>
            <td>
                <textarea rows="3" v-model="right.mR_Info"></textarea>
            </td>
        </tr>
        <tr>
            <td>权益是否自定义</td>
            <td>
                <input type="radio" name="isCustom" value="true" v-model="right.mR_IsCustom" />是
                <input type="radio" name="isCustom" value="false" v-model="right.mR_IsCustom" />否
            </td>
        </tr>
        <tr>
            <td>权益类型</td>
            <td>
                <select v-model="right.mR_RuleType">
                    <option value="0">请选择</option>
                    <option value="1">折扣</option>
                    <option value="2">餐饮（附赠早餐，午餐，晚餐）</option>
                    <option value="3">延时</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>权益规则</td>
            <td>
                <textarea rows="3" v-model="right.mR_Rule"></textarea>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button" value="添加权益" class="btn btn-success" @click="addRight" />
            </td>
        </tr>
    </table>
</template>

<script setup lang="ts"'>
    import {ref} from 'vue'
    import {useRouter} from 'vue-router'
    import axios from 'axios'
    const router=useRouter();
    const right=ref({
        "mR_Id": 0,
  "mR_Name": "",
  "mR_Info": "",
  "mR_IsCustom": true,
  "mR_RuleType": 0,
  "mR_Rule": ""
    });
    const addRight=()=>{
        var reg=/^([\u4e00-\u9fa5]{2,10})$/;
        if(!reg.test(right.value.mR_Name))
        {
            alert('权益名称请输2-10个汉字');
            return;
        }
        if(right.value.mR_Info.length>100)
        {
            alert('权益说明不能超过100个字符');
            return;
        }
        if(right.value.mR_Rule=="")
        {
            alert('请填写权益规则');
            return;
        }

        axios.post('https://localhost:44320/api/Member/RightAdd',{
  "mR_Name": right.value.mR_Name,
  "mR_Info": right.value.mR_Info,
  "mR_IsCustom": right.value.mR_IsCustom,
  "mR_RuleType": right.value.mR_RuleType,
  "mR_Rule": right.value.mR_Rule
        })
        .then(res=>{
            console.log(res.data);
            if(res.data>0)
            {
                alert('添加成功');
                router.push('/showRight');
            }
            else{alert('添加失败');}
        })
        .catch(error=>{
            console.log(error);
        })
    }


</script>